#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(register int i = (a);i <= (b);++i)
#define per(i,a,b) for(register int i = (a);i >= (b);--i)  
typedef long long ll;
typedef unsigned long long ull;
using std::string;using std::cin;using std::cout;

int n,m,p[105],tot,num[1050],map[105];
char ch;
int dp[105][105][105],ans;

inline int count(int x){
    int ans = 0;
    while(x){
        if(x&1) ++ans;
        x >>= 1;
    }
    return ans;
}

int main(){
    std::ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //freopen("in.in", "r", stdin);
    cin >> n >> m;
    rep(i,1,n) rep(j,1,m){
        cin >> ch;
        if(ch == 'H') map[i] |= (1 << (j-1));
    }
    rep(i,1,(1<<m)-1) if(!(i & (i<<1)) && !(i & (i<<2))) p[++tot] = i;
    rep(i,1,tot) num[p[i]] = count(p[i]);
    rep(i,1,m) rep(j,1,tot){//特判第一行
        if(p[j] & map[1]) continue;//保证是平原
        dp[1][j][0] = num[ p[j] ];
    }
    rep(j,1,tot){
        if(p[j] & map[2]) continue;//保证是平原
        rep(k,1,tot){
            if(p[j] & p[k]) continue;//保证不误伤
            if(p[k] & map[1]) continue;//保证前一行有方案
            dp[2][j][k] = dp[1][k][0] + num[p[j]];
        }
    }
    p[0] = 0;
    rep(i,3,n){
        rep(j,0,tot){
            if(p[j] & map[i]) continue;//保证是平原
            rep(k,0,tot){
                if(p[k] & p[j]) continue;//不与前一行误伤
                if(p[k] & map[i-1]) continue;//前一行有该方案
                rep(kk,0,tot){
                    if(p[kk] & p[k] || p[kk] & p[j]) continue; //不误伤
                    if(p[kk] & map[i-2]) continue;//前一行的前一行有方案
                    dp[i][j][k] = std::max(dp[i][j][k],dp[i-1][k][kk] + num[ p[j] ]);
                    // cout << i << " " << p[j] << " " << p[k] << " " << p[kk] << " " << dp[i][ p[j] ][ p[k] ] << "\n";
                }
                ans = std::max(ans,dp[i][j][k]);
            }
        }
    }
    cout << ans << "\n";
    return 0;
}